package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.CommodityCatalog;
import cn.wolfcode.trip.base.mapper.CommodityCatalogMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ICommodityCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommodityCatalogServiceImpl implements ICommodityCatalogService {

    @Autowired
    private CommodityCatalogMapper commodityCatalogMapper;

    @Override
    public List<CommodityCatalog> list() {
        return commodityCatalogMapper.selectAll();
    }

    @Override
    public PageInfo query(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        List<CommodityCatalog> list = commodityCatalogMapper.selectAll();
        return new PageInfo(list);
    }

    @Override
    public void delete(Long id) {
        commodityCatalogMapper.deleteByPrimaryKey(id);
        commodityCatalogMapper.deleteRelation(id);
    }

    @Override
    public void save(CommodityCatalog commodityCatalog) {
        if (commodityCatalog.getId()!=null){
            commodityCatalogMapper.updateByPrimaryKey(commodityCatalog);
        }else if(commodityCatalog.getTitle()!=null && commodityCatalog.getTitle().trim().length()!=0){
            commodityCatalogMapper.insert(commodityCatalog);
        }else{
            throw new RuntimeException("标题不能为空");
        }
    }
}
